package com.ziyun.pop.mall.module.user.service.impl;



import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.service.common.protocol.ResultCode;
import com.service.common.utils.PasswordUtil;
import com.service.soa.service.impl.BaseServiceImpl;
import com.service.ucenter.constant.UcenterResult;
import com.service.ucenter.exception.UserException;
import com.ziyun.pop.mall.module.user.dao.PopUserBaseInfoDao;
import com.ziyun.pop.mall.module.user.dao.PopUserLoginDao;
import com.ziyun.pop.mall.module.user.dao.PopUserWalletDao;
import com.ziyun.pop.mall.module.user.entity.PopSysUser;
import com.ziyun.pop.mall.module.user.entity.UserWallet;
import com.ziyun.pop.mall.module.user.service.PopUserAdvanceService;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

/**
 * 修改用户预存款密码
 * @author husong
 *
 */
@Service
@Transactional
public class PopUserAdvanceServiceImpl extends BaseServiceImpl implements PopUserAdvanceService{
	
	@Autowired
	private PopUserLoginDao popUserLoginDao;
	
	@Autowired
	private PopUserWalletDao popUserWalletDao;
	@Autowired
	private PopUserBaseInfoDao popUserBaseInfoDao;
		
	@Autowired
	private HttpRequestLocal httpRequestLocal;
	
	/**
	 *修改预存款密码
	 */
	@Override
	public UcenterResult<?> saveAdvancePass(String mobile, String newPassword, String loginName) throws UserException {
		// 获取用户userId
	    Object PopUserId = httpRequestLocal.getSessionValue("popUserId");
	    //获取登录用户信息
	    PopSysUser popSysUser = popUserLoginDao.single(PopUserId);	    							
		if (org.springframework.util.StringUtils.isEmpty(popSysUser)) {
			throw new UserException(ResultCode.ACCOUNT_NOT_EXIST);
		}
		UserWallet wallet = popUserWalletDao.single(popSysUser.getMallUserId());
		//账号手机号
		String userMobile = popSysUser.getMobile();
		//手机号是否为注册手机号				
		if (StringUtils.isBlank(mobile) && StringUtils.isBlank(newPassword) && StringUtils.isBlank(userMobile)) {
			throw new UserException(ResultCode.PARAM_LESS);
		}
		if (!StringUtils.equals(mobile,userMobile)) {
			throw new UserException("手机号非注册手机号");
		}
		if (!StringUtils.equals(loginName,popSysUser.getUsername())) {
			throw new UserException("登录名错误");
		}
		// 生成加密预支付密码		
		newPassword = PasswordUtil.getMemberPassword(newPassword, wallet.getPasswordAccount(),
				String.valueOf(wallet.getCreatetime()));
		//重置预支付密码					
		UserWallet userWallet = UserWallet.builder().payPassword(newPassword).userId(popSysUser.getMallUserId()).build();
		popUserWalletDao.updateTemplateById(userWallet);									
		
		UcenterResult<?> result = UcenterResult.success();
		result.setMessage("密码修改成功");
		return result;
	}


	
	/**
	 * 验证是否设置 预存款（支付）密码
	 */
	@Override
	public UcenterResult<Boolean> isSetPass() throws UserException{
		// 获取用户userId
	    Object PopUserId = httpRequestLocal.getSessionValue("popUserId");
	    //获取登录用户信息
	    PopSysUser popSysUser = popUserLoginDao.single(PopUserId);
	    if(org.springframework.util.StringUtils.isEmpty(popSysUser)){
	    	throw new UserException(ResultCode.MERCHANTS_NON_EXISTENT);//商户不存在
	    }
		String dvancePassword = "";
		UserWallet userWallet = popUserWalletDao.getPopUserWallet(popSysUser.getMallUserId());
		if (userWallet != null) {
			dvancePassword = userWallet.getPayPassword();
		}

		boolean isSet = false;
		if (dvancePassword != null && !dvancePassword.equals("")) {
			isSet = true;
		}
		UcenterResult<Boolean> result = UcenterResult.success(isSet);
		if (isSet) {
			result.setMessage("支付密码已设置");
		} else {
			result.setMessage("支付密码尚未设置");
		}
		return result;		
	}
	
	
	/**
	 * 校验预存款（支付）密码
	 */
	@Override
	public UcenterResult<Boolean> checkAdvancePass(String payPassword) throws UserException {

		UcenterResult<Boolean> result = UcenterResult.success();

		if (StringUtils.isBlank(payPassword)) {
			throw new UserException(ResultCode.PASSWORD_ERROR);
		}		
		
		// 获取用户userId
	    Object PopUserId = httpRequestLocal.getSessionValue("popUserId");
	    //获取登录用户信息
	    PopSysUser popSysUser = popUserLoginDao.single(PopUserId);    
	    if(org.springframework.util.StringUtils.isEmpty(popSysUser)){
	    	throw new UserException(ResultCode.MERCHANTS_NON_EXISTENT);//商户不存在
	    }
	    //获取预支付密码
	    String dvancePassword = "";
	    UserWallet userWallet = popUserWalletDao.single(popSysUser.getMallUserId());
		if (userWallet != null) {
			dvancePassword = userWallet.getPayPassword();
		}		
		//校验密码
		payPassword = PasswordUtil.getMemberPassword(payPassword, userWallet.getPasswordAccount(),
				String.valueOf(userWallet.getCreatetime()));
		if(StringUtils.equals(payPassword, dvancePassword)){
			result.setData(true);
			result.setMessage("支付密码正确");
			return result;
		}else{
			throw new UserException(ResultCode.PASSWORD_ERROR);
		}
		
	}		
	

}
